from manim import *



class math4(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设}$f\left( x\right) $\text{在}$\left[ 0,1\right] $\text{上二阶可导, 且满足}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,5,0])
        text1 = Tex(r"$f''\left( x\right)  >0$\text{及}$\int _{0}^{1}f\left( x^{2}\right) dx=0,$\text{则:}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([14,5,0])
        text2 = Tex(r"$\left( A\right) f\left( \frac{2}{3}\right)  <0,2f\left( 0\right) +f\left( 1\right) >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,0,0])
        text3 = Tex(r"$\left( B\right) f\left( \frac{2}{3}\right)  <0,f\left( 0\right) +2f\left( 1\right) >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,0,0])
        text4 = Tex(r"$\left( C\right) f\left( \frac{1}{3}\right)  <0,2f\left( 0\right) +f\left( 1\right) >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        text5 = Tex(r"$\left( D\right) f\left( \frac{1}{3}\right)  <0,f\left( 0\right) +2f\left( 1\right) >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-5,0])
        self.play(Write(text),Write(text1),Write(text2),Write(text3),Write(text4),Write(text5),self.camera.frame.animate.scale(0.9))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([-25,23,0]) ,text1.animate().move_to([14,23,0])
                  ,text2.animate().move_to([-25,18,0]) ,text3.animate().move_to([25,18,0])
                   ,text4.animate().move_to([-25,13,0]) ,text5.animate().move_to([25,13,0]) )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.1,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([1.4, 23, 0])
        rectangle0=Rectangle(color=BLUE,fill_opacity=0.1,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle0.move_to([1.4, 23, 0])
        self.play(Create(rectangle),Create(rectangle0))
        
        text6 = Tex(r"\text{泰勒不等式}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([1.4,27.5,0])
        self.play(Write(text6))
        text7 = Tex(r"$f\left( x\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \left( x-x_{0}\right) +\dfrac{f''\left( \xi \right) }{2}\left( x-x_{0}\right) ^{2}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text7))
        rectangle1=Rectangle(color=RED,fill_opacity=0.3,width=4,height=5
                            ,stroke_color=RED,stroke_width=20)
        rectangle1.move_to([-35, 18, 0])
        rectangle2=Rectangle(color=GREEN,fill_opacity=0.3,width=4,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([-35, 13, 0])
        rectangle3=Rectangle(color=RED,fill_opacity=0.3,width=4,height=5
                            ,stroke_color=RED,stroke_width=20)
        rectangle3.move_to([15, 18, 0])
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=4,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([15, 13, 0])
        self.play(Create(rectangle1),Create(rectangle2),Create(rectangle3),Create(rectangle4))

        text8 = Tex(r"\text{取}$x_{0}=\dfrac{2}{3}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-2.5,0])
        text9 = Tex(r"\text{取}$x_{0}=\dfrac{1}{3}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-2.5,0])
        self.play(ReplacementTransform(rectangle4,text9),FadeOut(rectangle1,rectangle2),ReplacementTransform(rectangle3,text8))
        self.wait(0.2)
        text10 = Tex(r"$f\left( x\right) =f\left( \frac{2}{3}\right) +f'\left( \frac{2}{3}\right) \left( x-\frac{2}{3}\right) +\frac{f''\left( \xi_1 \right) }{2}\left( x-\frac{2}{3}\right) ^{2}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text11 = Tex(r"$f\left( x\right) =f\left( \frac{1}{3}\right) +f'\left( \frac{1}{3}\right) \left( x-\frac{1}{3}\right) +\frac{f''\left( \xi_2 \right) }{2}\left( x-\frac{1}{3}\right) ^{2}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(ReplacementTransform(text8,text10),ReplacementTransform(text7,text10),ReplacementTransform(text9,text11))
        self.wait(0.2)
        text12 = Tex(r"$f\left( x\right) \geq f\left( \frac{2}{3}\right) +f'\left( \frac{2}{3}\right) \left( x-\frac{2}{3}\right) $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text13 = Tex(r"$f\left( x\right) \geq f\left( \frac{1}{3}\right) +f'\left( \frac{1}{3}\right) \left( x-\frac{1}{3}\right) $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(ReplacementTransform(rectangle0,text12),ReplacementTransform(rectangle,text13))
        self.wait(0.2)

        rectangle5=Rectangle(color=BLUE,fill_opacity=0.3,width=20,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([20, 23, 0])
       
        self.play(Create(rectangle5),FadeOut(text10,text11))

        text14 = Tex(r"$f\left( x^2\right) \geq f\left( \frac{2}{3}\right) +f'\left( \frac{2}{3}\right) \left( x^2-\frac{2}{3}\right) $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        text15 = Tex(r"$f\left( x^2\right) \geq f\left( \frac{1}{3}\right) +f'\left( \frac{1}{3}\right) \left( x^2-\frac{1}{3}\right) $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(ReplacementTransform(text12,text14),ReplacementTransform(text13,text15))
        self.wait(0.2)
        





        text140 = Tex(r"$\int _{0}^{1}f\left( x^2\right)dx \geq \int _{0}^{1}f\left( \frac{2}{3}\right)dx +\int _{0}^{1}f'\left( \frac{2}{3}\right) \left( x^2-\frac{2}{3}\right) dx$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        text150 = Tex(r"$\int _{0}^{1}f\left( x^2\right)dx \geq \int _{0}^{1}f\left( \frac{1}{3}\right)dx +\int _{0}^{1}f'\left( \frac{1}{3}\right) \left( x^2-\frac{1}{3}\right) dx$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text140),Write(text150))

        rectangle6=Rectangle(color=BLUE,fill_opacity=0.2,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle6.move_to([1.4, 23, 0])
        
        self.play(Create(rectangle6))

        rectangle7=Rectangle(color=BLUE,fill_opacity=0.2,width=3,height=9
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle7.move_to([-12.5, -7.5, 0])
        
        self.play(Create(rectangle7))

        text1400 = Tex(r"$\int _{0}^{1}f\left( x^2\right)dx > \int _{0}^{1}f\left( \frac{2}{3}\right)dx +\int _{0}^{1}f'\left( \frac{2}{3}\right) \left( x^2-\frac{2}{3}\right) dx$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        text1500 = Tex(r"$\int _{0}^{1}f\left( x^2\right)dx > \int _{0}^{1}f\left( \frac{1}{3}\right)dx +\int _{0}^{1}f'\left( \frac{1}{3}\right) \left( x^2-\frac{1}{3}\right) dx$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(ReplacementTransform(text140,text1400),ReplacementTransform(text150,text1500),FadeOut(rectangle7,text14,text15))
        rectangle8=Rectangle(color=BLUE,fill_opacity=0.3,width=12,height=9
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle8.move_to([-20.5, -7.5, 0])
        self.play(ReplacementTransform(rectangle5,rectangle8))
        self.wait(0.2)
        text16 = Tex(r"$0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle8,UP,buff=1)
        text17 = Tex(r"$0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle8,DOWN,buff=1)
        self.play(Write(text16),Write(text17))

        rectangle9=Rectangle(color=BLUE,fill_opacity=0.3,width=12,height=9
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle9.move_to([-5, -7.5, 0])
        self.play(Create(rectangle9))
        text18 = Tex(r"$f\left( \frac{2}{3}\right)$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle9,UP,buff=1)
        text19 = Tex(r"$f\left( \frac{1}{3}\right)$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle9,DOWN,buff=1)
        self.play(Write(text18),Write(text19))
       


        rectangle10=Rectangle(color=BLUE,fill_opacity=0.3,width=22,height=9
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle10.move_to([15, -7.5, 0])
        self.play(Create(rectangle10))
        text20 = Tex(r"$-\frac{1}{3}f'\left( \frac{2}{3}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle10,UP,buff=1)
        text21 = Tex(r"$0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle10,DOWN,buff=1)
        self.play(Write(text20),Write(text21))


        rectangle1=Rectangle(color=RED,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=RED,stroke_width=20)
        rectangle1.move_to([-34.5, 18, 0])
        rectangle2=Rectangle(color=GREEN,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([-34.5, 13, 0])
        rectangle3=Rectangle(color=RED,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=RED,stroke_width=20)
        rectangle3.move_to([16, 18, 0])
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([16, 13, 0])
        self.play(Create(rectangle2),Create(rectangle4))
        self.play(Create(rectangle1),Create(rectangle3))
        self.play(FadeOut(rectangle1),FadeOut(rectangle3))
        self.wait()



